import Vue from 'vue';
import VueRouter from 'vue-router';
import index from '../views/index.vue'
import login from '../views/account/login.vue';

Vue.use(VueRouter)

const routes = [
	// 主页ss
	{
		path: '/',
		name: 'index',
		component: index
	},
	// 登录
	{
		path: '/account/login',
		name: 'login',
		component: login
	},
	// 忘记密码
	{
		path: '/account/forgot',
		name: 'forgot',
		component: () => import('../views/account/forgot.vue')
	},
	// 注册账号
	{
		path: '/account/register',
		name: 'register',
		component: () => import('../views/account/register.vue')
	},
	// 媒体图片
	{
		path: '/media/image',
		name: 'media_image',
		component: () => import('../views/media/image.vue')
	},
	// 音乐
	{
		path: '/media/music',
		name: 'media_music',
		component: () => import('../views/media/music.vue')
	},
	// 媒体视频
	{
		path: '/media/video',
		name: 'media_video',
		component: () => import('../views/media/video.vue')
	},
	// 文章路由
	{
		path: '/article/list',
		name: 'article_list',
		component: () => import('../views/article/list.vue')
	},
	{
		path: '/article/details',
		name: 'article_details',
		component: () => import('../views/article/details.vue')
	},
	// 浏览网站
	// 收藏路由
	{
		path: '/user/collect',
		name: 'collect_list',
		component: () => import('../views/user/collect.vue')
	},
	// 我的消息
	{
		path: '/user/messages',
		name: 'user_messages',
		component: () => import('../views/user/messages.vue')
	},






	// 公告路由
	{
		path: '/notice/list',
		name: 'notice_list',
		component: () => import('../views/notice/list.vue')
	},
	{
		path: '/notice/details',
		name: 'notice_details',
		component: () => import('../views/notice/details.vue')
	},
	// 联系我们路由
	{
		path: '/contact',
		name: 'contact',
		component: () => import('../views/contact/index.vue')
	},
	
	
		
	
		
		// 会议信息列表路由
	{
		path: '/meeting_information/list',
		name: '/meeting_information_list',
		component: () => import('../views/meeting_information/list.vue')
	},
	
		// 会议信息详情路由
	{
		path: '/meeting_information/details',
		name: '/meeting_information_details',
		component: () => import('../views/meeting_information/details.vue')
	},
			// 专家参会添加路由
	{
		path: '/expert_participation/edit',
		name: '/expert_participation_edit',
		component: () => import('../views/expert_participation/edit.vue')
	},
	
	
		
	
		
	
		
	
			// 用户参会添加路由
	{
		path: '/user_participation/edit',
		name: '/user_participation_edit',
		component: () => import('../views/user_participation/edit.vue')
	},
	// 用户参会报名路由（新样式）
	{
		path: '/user_participation/register',
		name: '/user_participation_register',
		component: () => import('../views/user_participation/register.vue')
	},
	// 用户参会支付页面
	{
		path: '/user_participation/payment',
		name: '/user_participation_payment',
		component: () => import('../views/user_participation/payment.vue')
	},
	// 用户参会成功页面
	{
		path: '/user_participation/success',
		name: '/user_participation_success',
		component: () => import('../views/user_participation/success.vue')
	},
	
	
		
	
		
	
		
	
		
	
		
	
		
	
		
	
	
	// 用户路由
	{
		path: '/user/index',
		name: 'user_index',
		component: () => import('../views/user/index.vue')
	},
	// 用户中心（兼容路径）
	{
		path: '/user/center',
		name: 'user_center',
		component: () => import('../views/user/participation.vue')
	},
	// 基本信息
	{
		path: '/user/info',
		name: 'user_info',
		component: () => import('../views/user/info.vue')
	},
	// 我的报名
	{
		path: '/user/participation',
		name: 'user_participation',
		component: () => import('../views/user/participation.vue')
	},
	// 找回密码
	{
		path: '/user/password',
		name: 'user_password',
		component: () => import('../views/user/password.vue')
	},

	// 搜索
	{
		path: '/search',
		name: 'search',
		component: () => import('../views/search/index.vue')
	},
	// 局部搜索
	{
		path: '/search/details',
		name: 'search_details',
		component: () => import('../views/search/details.vue')
	}
]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})

// 路由守卫：在每次路由跳转前恢复登录状态
router.beforeEach((to, from, next) => {
	// 确保路由跳转正常进行，不阻塞
	next();
	
	// 在路由跳转后（异步）尝试恢复登录状态
	// 这样可以避免在 router.app 未初始化时出错，也不影响路由跳转
	Vue.nextTick(() => {
		try {
			// 只有在 router.app 已初始化时才恢复登录状态
			if (router.app && typeof $.db !== 'undefined' && $.db.get) {
				const token = $.db.get("token");
				
				if (token) {
					const store = router.app.$store;
					// 如果有 token 但 store 中没有用户信息，尝试恢复
					if (store && (!store.state.user.user_id || store.state.user.user_id === 0)) {
						router.app.$get_user((user) => {
							if (user && user.user_id) {
								console.log('路由守卫：恢复登录状态成功', user);
							}
						});
					}
				}
			}
		} catch (e) {
			// 静默处理错误，不影响路由跳转
			console.warn('路由守卫中恢复登录状态失败:', e);
		}
	});
});

router.afterEach((to, from) => {
	// 根据语言设置更新页面标题
	const language = router.app && router.app.$store ? router.app.$store.state.web.language : 'zh';
	const title = language === 'zh' 
		? "学术会议管理系统-home"
		: "Academic Conference Management System - Home";
	document.title = title;
	document.logo = language === 'zh' ? "学术会议管理系统" : "Academic Conference Management System";
})

export default router
